//
//  BM-CollectionViewLayout.m
//  15-Homework
//
//  Created by GivenBmli on 16/8/3.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "BM-CollectionViewLayout.h"

@interface BM_CollectionViewLayout ()
@property (nonatomic, strong) NSMutableArray *att_arr;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat WIDTH;
@end

@implementation BM_CollectionViewLayout
- (instancetype)init {
    if (self = [super init]) {
        _att_arr = [NSMutableArray array];
        _height = 44;
    }
    return self;
}
- (void)prepareLayout {
    [super prepareLayout];
    _WIDTH = 0;
    for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self widthForItemAtIndexPath:indexPath height:_height];
        
        att.frame = CGRectMake(_WIDTH, 0, width, _height);
        [_att_arr addObject:att];
        
        _WIDTH = _WIDTH + width + 20;
    }
    
}
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _att_arr;
}
- (CGFloat)att_width {
    return 44;
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(_WIDTH, 44);
}
@end
